package com.problem.leetcode;

/**
 * @author didi
 * @DESCRIPTION
 * @create 2019-04-20 15:53
 **/

public class Problem19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode tmpHead = new ListNode(-1);
        tmpHead.next = head;

        ListNode first = tmpHead, second = first.next, index = second;
        while (n-- > 0 && index != null) {
            index = index.next;
        }

        while (index != null) {
            first = first.next;
            second = second.next;
            index = index.next;
        }
        first.next = second.next;
        return tmpHead.next;
    }

    public static void main(String[] args) {

        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        // head.next.next = new ListNode(3);
        // head.next.next.next = new ListNode(4);
        // head.next.next.next.next = new ListNode(5);

        ListNode tmp = new Problem19().removeNthFromEnd(head, 2);
        System.out.println(tmp);

    }

}
